function Promise(executor){
  this.PromiseState = 'pending'
  this.PromiseResult = null
  // this.callback = {}
  this.callbacks = []
  const self = this

  function resolve(data){
    if(self.PromiseState!=='pending') return
    self.PromiseState='fulfilled'
    self.PromiseResult=data;
    console.log('resolved');
    // 异步任务中调用成功时,执行then中定义的成功回调函数
    if(self.callbacks && self.callbacks.length>0) self.callbacks.forEach(e => e.onResolved(data));
  }

  function reject(data){
    if(self.PromiseState!=='pending') return
    self.PromiseState='rejected'
    self.PromiseResult=data;
    console.log('rejected');
    // 异步任务中调用失败时,执行then中定义的失败回调函数
    if(self.callbacks && self.callbacks.length>0) self.callbacks.forEach(e => e.onRejected(data));
  }

  try{
    executor(resolve, reject)
  }catch(e){
    reject(e)
  }
}

Promise.prototype.then = function(onResolved, onRejected) {
  if('fulfilled'===this.PromiseState){
    onResolved(this.PromiseResult)
  }
  if('rejected'===this.PromiseState){
    onRejected(this.PromiseResult)
  }
  if('pending'===this.PromiseState){
    // 异步调用时使用
    console.log('pending...');
    // 多次执行then时会覆盖前面执行的函数,只保留最后一次,因此这个实现无法保证与标准版本一致
    /*
    this.callback = {
      onResolved,
      onRejected
    }
    */
    this.callbacks.push({
      onResolved,
      onRejected
    })
  }
};